home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Java / JavaRadioStation / Source / com / apple / jens / radio / ShuffleMusicDirector.java < prev    next >
Encoding:
Java Source  |  2000-09-28  |  3.6 KB  |  90 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        ShuffleMusicDirector.java
  3.     
  4.     Copyright:     © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
  5.     
  6.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  7.                 ("Apple") in consideration of your agreement to the following terms, and your
  8.                 use, installation, modification or redistribution of this Apple software
  9.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  10.                 please do not use, install, modify or redistribute this Apple software.
  11.  
  12.                 In consideration of your agreement to abide by the following terms, and subject
  13.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  14.                 copyrights in this original Apple software (the "Apple Software"), to use,
  15.                 reproduce, modify and redistribute the Apple Software, with or without
  16.                 modifications, in source and/or binary forms; provided that if you redistribute
  17.                 the Apple Software in its entirety and without modifications, you must retain
  18.                 this notice and the following text and disclaimers in all such redistributions of
  19.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  20.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  21.                 Apple Software without specific prior written permission from Apple.  Except as
  22.                 expressly stated in this notice, no other rights or licenses, express or implied,
  23.                 are granted by Apple herein, including but not limited to any patent rights that
  24.                 may be infringed by your derivative works or by other works in which the Apple
  25.                 Software may be incorporated.
  26.  
  27.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  28.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  29.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  31.                 COMBINATION WITH YOUR PRODUCTS.
  32.  
  33.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  34.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  35.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  37.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  38.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  39.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40.                 
  41.     Change History (most recent first):
  42.  
  43. */
  44.  
  45.  
  46. package com.apple.jens.radio;
  47.  
  48. import com.apple.jens.mp3.MP3File;
  49. import java.io.*;
  50. import java.util.*;
  51.  
  52.  
  53. /** A MusicDirector that plays all MP3 files in its directory in random order.
  54.     It syncs with the current directory contents each time through, so it's
  55.     safe to add/remove/rename files while it's playing.
  56.     
  57. */
  58.     
  59. public class ShuffleMusicDirector extends SequentialMusicDirector {
  60.  
  61.     public synchronized MP3File getNextTrack( ) {
  62.         // When shuffling, it's good to incorporate playlist changes immediately.
  63.         if( sync() )
  64.             reset();
  65.         return super.getNextTrack();
  66.     }
  67.  
  68.     /** Randomly rearranges all the elements of fFiles */
  69.     protected void reset( ) {
  70.         super.reset();
  71.         // Simply swap each element in turn with a randomly-chosen lower-numbered one:
  72.         for( int i=fFiles.length-1; i>0; i-- ) {
  73.             int j = random(i);
  74.             MP3File temp = fFiles[i];
  75.             fFiles[i] = fFiles[j];
  76.             fFiles[j] = temp;
  77.         }
  78.     }
  79.     
  80.     
  81.     /** Returns a random integer from the interval [0..range). */
  82.     private int random( int range ) {
  83.         return Math.abs(fRandom.nextInt()) % range;
  84.     }
  85.     
  86.     
  87.     private Random fRandom = new Random();
  88.     
  89. }
  90.